package com.starlight.ts_clouddev.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starlight.ts_clouddev.infrastructure.entity.CloudInstance;
import com.starlight.ts_clouddev.infrastructure.mapper.CloudInstanceMapper;
import com.starlight.ts_clouddev.service.CloudInstanceService;
import com.starlight.ts_clouddev.util.TotalChargeCalculator;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author yfy2001
* @description 针对表【cloud_instance】的数据库操作Service实现
* @createDate 2023-05-18 15:09:14
*/
@Service
public class CloudInstanceServiceImpl extends ServiceImpl<CloudInstanceMapper, CloudInstance>
    implements CloudInstanceService{
    @Resource
    private CloudInstanceMapper cloudInstanceMapper;
    @Resource
    private TotalChargeCalculator totalChargeCalculator;

    @Override
    public List<CloudInstance> listByUserId(Long userId) {
        return cloudInstanceMapper.selectList(new LambdaQueryWrapper<CloudInstance>()
                .isNotNull(CloudInstance::getId)
                .eq(CloudInstance::getUserId,userId));
    }

    @Override
    public CloudInstance getDefaultInstanceByUserId(Long userId) {
        return cloudInstanceMapper.selectOne(new LambdaQueryWrapper<CloudInstance>()
                .eq(CloudInstance::getUserId,userId)
                .eq(CloudInstance::getMirrorId,1L)
                .eq(CloudInstance::getCpu,2L)
                .eq(CloudInstance::getMemory,4L)
                .eq(CloudInstance::getStorage,100L)
                .eq(CloudInstance::getBandwidth,1L)
                .orderByAsc(CloudInstance::getCreated)
                .last("limit 1"));
    }

    @Override
    public Long calculateUpgradeTotalCharge(CloudInstance cloudInstance) {
        Long cpu;
        Long memory;
        Long storage;
        Long bandwidth;
        long time;
        if (cloudInstance.getId()!=null){
            //升级虚拟机，按照差值计算价格
            CloudInstance oldInstance=cloudInstanceMapper.selectById(cloudInstance.getId());
            cpu=cloudInstance.getCpu()-oldInstance.getCpu();
            memory= cloudInstance.getMemory()-oldInstance.getMemory();
            storage= cloudInstance.getStorage()-oldInstance.getStorage();
            bandwidth= cloudInstance.getBandwidth()-oldInstance.getBandwidth();
            time=(oldInstance.getDeadline().getTime()-System.currentTimeMillis())/(1000L *60*60*24*30);
        }else {
            cpu=cloudInstance.getCpu();
            memory= cloudInstance.getMemory();
            storage= cloudInstance.getStorage();
            bandwidth= cloudInstance.getBandwidth();
            time=1L;
        }
        return totalChargeCalculator.Calculate(cpu,memory,storage,bandwidth,time);
    }

    @Override
    public Long calculateRenewTotalCharge(Long cloudInstanceId,Long deltaTime) {
        Long cpu;
        Long memory;
        Long storage;
        Long bandwidth;
        long time;
        CloudInstance cloudInstance=cloudInstanceMapper.selectById(cloudInstanceId);
        cpu=cloudInstance.getCpu();
        memory= cloudInstance.getMemory();
        storage= cloudInstance.getStorage();
        bandwidth= cloudInstance.getBandwidth();
        time=deltaTime;
        return totalChargeCalculator.Calculate(cpu,memory,storage,bandwidth,time);
    }
}




